﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
    void RegisterRoutes(RouteCollection routes)
    {
        //khai báo định tuyến cho trang Default.aspx
        routes.MapPageRoute(
            "Trang-Chu",//tên định tuyến
            "Trang-Chu/{ViTri}",//khai báo tham số cho URL
            "~/admincp/Default.aspx"//tên trang web
        );
        routes.MapPageRoute(
             "Quan-Ly-Chung-Loai",
             "Quan-Ly-Chung-Loai/{ViTri}",
             "~/admincp/Categories.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-Nhan-Vien-Khach-Hang",
            "Quan-Ly-Nhan-Vien-Khach-Hang/{ViTri}/{id}",
            "~/admincp/Employees.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-Xuat-Xu",
            "Quan-Ly-Xuat-Xu/{ViTri}",
            "~/admincp/Origins.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-Chuc-Vu",
            "Quan-Ly-Chuc-Vu/{ViTri}",
            "~/admincp/Positions.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-San-Pham",
            "Quan-Ly-San-Pham/{ViTri}/{id}",
            "~/admincp/Products.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-Nha-Cung-Cap",
            "Quan-Ly-Nha-Cung-Cap/{ViTri}",
            "~/admincp/Suppliers.aspx"
            );
        routes.MapPageRoute(
            "Quan-Ly-Dat-Hang-Thanh-Toan",
            "Quan-Ly-Dat-Hang-Thanh-Toan/{ViTri}/{id}",
            "~/admincp/Order-Payment.aspx"
            );
        routes.MapPageRoute(
            "Chaomung",
            "Chaomung/{id}.aspx",
            "~/chaomung.aspx"
            );
    }
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);
        Application["FCKeditor:UserFilesPath"] = "../../../../../img/";
        //Khởi tạo giá trị số người dùng online và số lượt truy cập

        
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
        Exception ex = HttpContext.Current.Server.GetLastError();
        if (ex.InnerException != null)
        {
            ex = ex.InnerException;
        }
        if (ex is HttpException)
        {
            if (((HttpException)ex).GetHttpCode() == 404)
            {
                Response.Redirect("~/Error.html");
            }
            else
            {
                Response.Redirect("~/Error.html");
            }
        }

        HttpContext.Current.Server.ClearError();
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        DataTable dtCart = new DataTable();
        dtCart.Columns.Add("ProductID", typeof(int));
        dtCart.Columns.Add("Productname", typeof(string));
        dtCart.Columns.Add("CategoryID", typeof(string));
        dtCart.Columns.Add("Supplierid", typeof(int));
        dtCart.Columns.Add("Photopath", typeof(string));
        dtCart.Columns.Add("UnitPrice", typeof(double));
        dtCart.Columns.Add("Quantity", typeof(int));
        dtCart.Columns["Quantity"].DefaultValue = 1;
        dtCart.Columns.Add("TotalAmount", typeof(double), "UnitPrice*Quantity");
        dtCart.PrimaryKey = new DataColumn[] { dtCart.Columns[0] };
        Session["Cart"] = dtCart;
        int count_visit = 0;
        //Kiểm tra file count_visit.txt nếu không tồn  tại thì
        if (System.IO.File.Exists(Server.MapPath("~/count_visit.txt")) == false)
        {
            count_visit = 1;
        }
        // Ngược lại thì
        else
        {
            // Đọc dử liều từ file count_visit.txt
            System.IO.StreamReader read = new System.IO.StreamReader(Server.MapPath("~/count_visit.txt"));
            count_visit = int.Parse(read.ReadLine());
            read.Close();
            // Tăng biến count_visit thêm 1
            count_visit++;
        }
        // khóa website
        Application.Lock();
        // gán biến Application count_visit
        Application["count_visit"] = count_visit;
        // Mở khóa website
        Application.UnLock();
        // Lưu dử liệu vào file  count_visit.txt
        System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/count_visit.txt"));
        writer.WriteLine(count_visit);
        writer.Close();
        if (Session["online"] == null)
        {
            Session["online"] = 1;
        }
        else
        {
            Session["online"] = int.Parse(Session["online"].ToString()) + 1;
        }
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        Session["online"] = int.Parse(Session["online"].ToString()) - 1;
    }
       
</script>
